//
//  RecommendViewController.swift
//  HiBook
//
//  Created by Apple on 16/7/27.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit

class RecommendViewController: CommonViewController {


    var topCarousel: LPCarouselView!
    
    @IBOutlet weak var botCarousel: iCarousel!
    let carouselModel = CarouselModel()
    
    var urlArray = [String]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "书推"
        self.view.backgroundColor = UIColor(red: 242/255, green: 235/255, blue: 217/255, alpha: 1)
        
        
        self.botCarousel.delegate = carouselModel
        self.botCarousel.dataSource = carouselModel
        
        carouselModel.target = self
        
        self.botCarousel.type = .Rotary
        
        Just.get("http://\(MAIN_IP):8080/hibook/getIndexJson.do") { (r) in
            dispatch_async(dispatch_get_main_queue(), { 
                if(r.ok){
                    guard let jsonArray = r.json as? NSArray else{
                        return
                    }
                    for json in jsonArray{

                        let result = Just.get(json.valueForKey("ibImageUrl") as! String)
                        let image = UIImage(data: result.content!)
                        let imageView = UIImageView(image: image)
                        imageView.frame = CGRect(x: 0, y: 40, width: 200, height: 280)
                    
                        self.carouselModel.imageArray.append(image!)
                        self.carouselModel.imageViewArray.append(imageView)
                        self.urlArray.append(json.valueForKey("itImageUrl") as! String)
                    }
                    
                }
                if(self.urlArray.count>0){
                    print("成功了")
                    print(self.urlArray.count)
                    self.topCarousel = LPCarouselView(frame:  CGRectMake(0, 64, SCREEN_WIDTH, 190), placeholderImage: nil, images: { () -> [AnyObject]! in
                        return self.urlArray
                        }, titles: nil, selectedBlock: nil)
                    self.view.addSubview(self.topCarousel!)

                } else {
                    print("还没有")
                }
                
                self.botCarousel.reloadData()
        

            })

            
        }
        
     
        // Do any additional setup after loading the view.
    }

    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
